package com.zxd.threads.thread_pool.runnable_task;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * ClassName:Test
 * Package:com.zxd.threads.thread_pool
 *
 * @Date:2023/9/26 21:18
 * @Author:zxde
 */
public class Test {
    public static void main(String[] args) {
        //corePoolSize = 3 ;核心线程是3
        // maximumPoolSize = 5 最大线程数量是5（临时线程2）
        //当在线程池最大线程被占用，且任务队列已经满的时候4，再提交线程执行RejectedExecutionHandler
        //第三个参数是临时线程存活的时间
        ExecutorService threadPoolExecutor = new ThreadPoolExecutor(3, 5, Integer.MAX_VALUE,
                TimeUnit.SECONDS, new ArrayBlockingQueue<>(4), new ThreadPoolExecutor.AbortPolicy());
        //核心线程处理
        threadPoolExecutor.execute(new MyRunnable());
        threadPoolExecutor.execute(new MyRunnable());
        threadPoolExecutor.execute(new MyRunnable());
        //放入任务队列
        threadPoolExecutor.execute(new MyRunnable());
        threadPoolExecutor.execute(new MyRunnable());
        threadPoolExecutor.execute(new MyRunnable());
        threadPoolExecutor.execute(new MyRunnable());
        //临时线程处理
        threadPoolExecutor.execute(new MyRunnable());
        threadPoolExecutor.execute(new MyRunnable());
        //RejectedExecutionHandler
        /** 在ThreadPoolExecutor中存在四种静态内部类；
         * 其都实现了RejectedExecutionHandler接口，可以作为ThreadPoolExecutor构造器的最后一个参数
         * 使用new ThreadPoolExecutor.CallerRunsPolicy(),即可返回相应的对象
         *          CallerRunsPolicy        超出线程由main线程执行
         *          DiscardOldestPolicy     遗弃存在时间最长的线程超出线程
         *          DiscardPolicy           丢弃超出线程
         *          AbortPolicy             不执行，直接报错
         */
        threadPoolExecutor.execute(new MyRunnable());

    }
}
